<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Attendance extends CI_Controller
{
	function __construct()
	{
		parent::__construct();		
		// check if user logged in
		if(!$this->ion_auth->logged_in()){
			redirect('auth/login');
		}
		$this->load->library('form_validation');
		$this->load->model('user/attendance_model');
		$this->load->helper('common_functions');
		$this->load->helper('html');
		
		//cheking of all attendance 
	}

	function index($year_id,$month_id)
	{		
		$meta['page_title'] = "Attendance Details";
		$data['server_time'] = $this->attendance_model->get_server_time();
		$data['attendance'] = $this->attendance_model->get_attendance_details();
		$data['daily_sheets']= $this->attendance_model->get_details($year_id,$month_id);		
		$yid = '20'.$year_id;
		$data['num'] = cal_days_in_month(CAL_GREGORIAN, $month_id, $yid);
			
		$data['year_lst'] = $this->attendance_model->get_worksheet_year();
		
		//get my Salary
		$data['my_salary'] = $this->attendance_model->get_salary_by_id();
		
		//check whether salary slip is generated or not
		$data['salary_slip'] = $this->attendance_model->get_salary_slip($year_id,$month_id);
		
		/*getting worksheet status*/
		$data['worksheet_status'] = $this->worksheet_status($this->session->userdata('user_id'),$month_id,$year_id);
		
		// check how many absent days		
		$search = array(
			'year_id' 	=> 	$year_id,
			'month_id'	=>	$month_id,			
			'user_id' 	=> 	$this->session->userdata('user_id'),
		);
		
		
		/*getting data from worksheet*/
		$data['fine'] = array(		
		'fine_amount' => $this->fine($this->session->userdata('user_id'),$month_id,$year_id,'amount'),
		'days' => $this->fine($this->session->userdata('user_id'),$month_id,$year_id,'days'),		
		);	
		
		/*value for the widget area in attendance*/
		$data['global_data'] = array(
		'working_days' => working_days($month_id,$year_id),
		'total_working_hour' => working_days($month_id,$year_id) * $this->attendance_model->get_working_hr(),
		'min_working_hour' => $this->attendance_model->get_working_hr(),
		);		
		$data['settings'] = $this->attendance_model->get_settings();
		
		if($data['settings']){}		
		$this->attendance_model->add_absent($search);						
		$this->load->view('common/header', $meta);
		$this->load->view('user/attendance',$data);
		$this->load->view('common/footer');	
		
	}
	
	function edit_attendance()
	{		
		$this->data['users'] = $this->ion_auth->users()->result();
		$meta['page_title'] = "Attendance Details";
		$this->load->view('common/header', $meta);
		$this->load->view('user/edit-attendance',$this->data);
		$this->load->view('common/footer');
	}
	
	function get_particular_attendance(){
		$get_id = $_REQUEST['user_id'];
		$get_date = $_REQUEST['date_id'];
		
		//exploding the date
		$date = $get_date;
		$explode = explode('-',$date);
		$year_id = trim($explode[0],'20');
		$month_id = $explode[1];
		$date_id = $explode[2];
		
		$search = array(
		'year_id' => $year_id,
		'month_id' =>$month_id,
		'date_id' => $date_id,
		'user_id' => $get_id,		
		);	
		
		$times = $this->attendance_model->get_time_by_id($search);
		if(!empty($times)){		
		echo $msg = $times['entry_time'].','.$times['lunch_in_time'].','.$times['lunch_out_time'].','.$times['exit_time'];
		}
		//	print_r($times);
		//redirect('user/attendance/edit_attendance',$times);	
	}
	
	// update function
	
	function update_entry_time()
	{
		$get_id     = $_REQUEST['user_id'];
		$get_date   = $_REQUEST['date_id'];
		$entry_time = $_REQUEST['entry_time'];
		// exit;
		//exploding the date
		$date       = $get_date;
		$explode    = explode('-',$date);
		$year_id    = trim($explode[0],'20');
		$month_id   = $explode[1];
		$date_id    = $explode[2];

		$search     = array(
			'year_id' => $year_id,
			'month_id'=>$month_id,
			'date_id' => $date_id,
			'user_id' => $get_id,
		);
		$search_val = $this->attendance_model->get_time_by_id($search);	
		//print_r($search);
		//exit;	
		if(empty($search_val))
		{
			$this->attendance_model->insert_edited_time($search,$entry_time);	
			echo $entry_time;
		}
		/*Check for lunch in time*/
		elseif(empty($search_val['lunch_out_time']) && ($search_val['exit_time']))
		{
			$this->attendance_model->update_entry_time($search,$entry_time);
				
				$total_time = strtotime($search_val['lunch_in_time']) - strtotime($entry_time);
				$hours      = floor($total_time / 3600);
				$minutes    = intval(($total_time / 60) % 60);
				//$hours.":".$minutes;
				//return $hours.":".$minutes;
				$working_time = $hours.":".$minutes;
				//exit;
				$this->attendance_model->update_working_hr($search,$working_time);
				echo $entry_time; 			
		}
		/*Check for all Attendance time except exit time*/
		elseif(empty($search_val['exit_time']))
		{
			$this->attendance_model->update_entry_time($search,$entry_time);
				
				$total_time = strtotime($search_val['lunch_out_time']) - strtotime($search_val['lunch_in_time']) - strtotime($entry_time);
				$hours      = floor($total_time / 3600);
				$minutes    = intval(($total_time / 60) % 60);
				
				$working_time = $hours.":".$minutes;
				
				$this->attendance_model->update_working_hr($search,$working_time);
				echo $entry_time; 			
		}
		/*Check for all Attendance time*/
		elseif(isset($search_val['lunch_in_time']) && ($search_val['lunch_out_time']) && ($search_val['exit_time']))
		{
			$this->attendance_model->update_entry_time($search,$entry_time);
				
				$total_time = (strtotime($search_val['exit_time']) - strtotime($search_val['lunch_out_time'])) + (strtotime($search_val['lunch_in_time']) - strtotime($entry_time));
				$hours      = floor($total_time / 3600);
				$minutes    = intval(($total_time / 60) % 60);

				$working_time = $hours.":".$minutes;
				
				$this->attendance_model->update_working_hr($search,$working_time);
				echo $entry_time; 			
		}
		else{
			$this->attendance_model->update_entry_time($search,$entry_time);	
			echo $entry_time;
			
		}

	}
	
	function update_lunch_in_time(){
		
		$get_id     = $_REQUEST['user_id'];
		$get_date   = $_REQUEST['date_id'];
		$lunch_in_time = $_REQUEST['lunch_in_time'];
		// exit;

		//exploding the date
		$date       = $get_date;
		$explode    = explode('-',$date);
		$year_id    = trim($explode[0],'20');
		$month_id   = $explode[1];
		$date_id    = $explode[2];

		$search     = array(
			'year_id' => $year_id,
			'month_id'=>$month_id,
			'date_id' => $date_id,
			'user_id' => $get_id,
		);
		$search_val = $this->attendance_model->get_time_by_id($search);	
			
		if(empty($search_val['lunch_out_time']) && ($search_val['exit_time']))
			{
				$this->attendance_model->update_lunch_in_time($search,$lunch_in_time);
				
				$total_time = strtotime($lunch_in_time) - strtotime($search_val['entry_time']);
				$hours      = floor($total_time / 3600);
				$minutes    = intval(($total_time / 60) % 60);
				//$hours.":".$minutes;
				//return $hours.":".$minutes;
				 $working_time = $hours.":".$minutes;
				//exit;
				$this->attendance_model->update_working_hr($search,$working_time);
				echo $lunch_in_time;
			}
			
		
		elseif(empty($search_val['exit_time']))
		{
			$this->attendance_model->update_lunch_in_time($search,$lunch_in_time);
				
				$total_time = strtotime($lunch_in_time) - strtotime($search_val['entry_time']);
				$hours      = floor($total_time / 3600);
				$minutes    = intval(($total_time / 60) % 60);
				
				$working_time = $hours.":".$minutes;
				
				$this->attendance_model->update_working_hr($search,$working_time);
				echo $lunch_in_time; 			
		}
		elseif(isset($search_val['entry_time']) && ($search_val['lunch_out_time']) && ($search_val['exit_time']))
		{
			$this->attendance_model->update_lunch_in_time($search,$lunch_in_time);
				
				$total_time = (strtotime($search_val['exit_time']) - strtotime($search_val['lunch_out_time'])) + (strtotime($lunch_in_time)) - (strtotime($search_val['entry_time']));     
				$hours      = floor($total_time / 3600);
				$minutes    = intval(($total_time / 60) % 60);

				$working_time = $hours.":".$minutes;
				
				$this->attendance_model->update_working_hr($search,$working_time);
				echo $lunch_in_time; 			
		}
		
	}
	
	function update_lunch_out_time(){
		
		$get_id     = $_REQUEST['user_id'];
		$get_date   = $_REQUEST['date_id'];
		$lunch_out_time = $_REQUEST['lunch_out_time'];
		// exit;

		//exploding the date
		$date       = $get_date;
		$explode    = explode('-',$date);
		$year_id    = trim($explode[0],'20');
		$month_id   = $explode[1];
		$date_id    = $explode[2];

		$search     = array(
			'year_id' => $year_id,
			'month_id'=>$month_id,
			'date_id' => $date_id,
			'user_id' => $get_id,
		);
		$search_val = $this->attendance_model->get_time_by_id($search);	
			
		if(empty($search_val['exit_time']))
		{
			$this->attendance_model->update_lunch_out_time($search,$lunch_out_time);
				
				$total_time = strtotime($search_val['entry_time']) - strtotime($search_val['lunch_in_time']);
				$hours      = floor($total_time / 3600);
				$minutes    = intval(($total_time / 60) % 60);
				
				$working_time = $hours.":".$minutes;
				
				$this->attendance_model->update_working_hr($search,$working_time);
				echo $lunch_out_time; 			
		}
		elseif(isset($search_val['entry_time']) && ($search_val['exit_time']))
		{
			$this->attendance_model->update_lunch_out_time($search,$lunch_out_time);
				
				$total_time = strtotime($search_val['exit_time']) - strtotime($search_val['entry_time']);     
				$hours      = floor($total_time / 3600);
				$minutes    = intval(($total_time / 60) % 60);

				$working_time = $hours.":".$minutes;
				
				$this->attendance_model->update_working_hr($search,$working_time);
				echo $lunch_out_time; 			
		}
		elseif(isset($search_val['entry_time']) && ($search_val['lunch_in_time']) && ($search_val['exit_time']))
		{
			$this->attendance_model->update_lunch_out_time($search,$lunch_out_time);
				
				$total_time = (strtotime($search_val['exit_time']) - strtotime($lunch_out_time)) + (strtotime($search_val['lunch_in_time'])) - (strtotime($search_val['entry_time']));     
				$hours      = floor($total_time / 3600);
				$minutes    = intval(($total_time / 60) % 60);

				$working_time = $hours.":".$minutes;
				
				$this->attendance_model->update_working_hr($search,$working_time);
				echo $lunch_out_time; 			
		}
		
	}
	
	
	function update_exit_time(){
		
		$get_id     = $_REQUEST['user_id'];
		$get_date   = $_REQUEST['date_id'];
		$exit_time = $_REQUEST['exit_time'];
		// exit;

		//exploding the date
		$date       = $get_date;
		$explode    = explode('-',$date);
		$year_id    = trim($explode[0],'20');
		$month_id   = $explode[1];
		$date_id    = $explode[2];

		$search     = array(
			'year_id' => $year_id,
			'month_id'=>$month_id,
			'date_id' => $date_id,
			'user_id' => $get_id,
		);
		$search_val = $this->attendance_model->get_time_by_id($search);		
		if(isset($search_val['entry_time']) && ($search_val['lunch_in_time']) && ($search_val['lunch_out_time']))
			{
				$this->attendance_model->update_exit_time($search,$exit_time);
				
				$total_time = (strtotime($exit_time) - strtotime($search_val['lunch_out_time'])) + (strtotime($search_val['lunch_in_time'])) - (strtotime($search_val['entry_time']));  
				$hours      = floor($total_time / 3600);
				$minutes    = intval(($total_time / 60) % 60);
				//$hours.":".$minutes;
				//return $hours.":".$minutes;
				 $working_time = $hours.":".$minutes;
				//exit;
				$this->attendance_model->update_working_hr($search,$working_time);
				echo $exit_time;
			}
		
	}

	function get_month_id()
	{
		return date('m');
	}

	function get_year_id()
	{
		return date('y');
	}

	function get_date_id()
	{
		return date('d');
	}

	/*Enter Entry Time for attendance */
	function insert_entry_time()
	{
		$insert['year_id'] = $this->get_year_id();
		$insert['month_id'] = $this->get_month_id();
		$insert['date_id'] = $this->get_date_id();
		$insert['user_id'] = $this->session->userdata('user_id');
		//$insert['entry_time'] = $_REQUEST['time'];

		$insert['entry_time'] = $this->attendance_model->get_server_time();

		$insert['entry_ip'] = $_SERVER['REMOTE_ADDR'];
		echo $this->attendance_model->insert_entry_time($insert);
	}

	function check_entry_time()
	{
		$check['month_id'] = $this->get_month_id();
		$check['date_id'] = $this->get_date_id();
		$check['user_id'] = $this->session->userdata('user_id');
		echo $time = $this->attendance_model->check_entry_time($check);
	
	}
	
	function check_id()
	{
		//echo 'asddasasdads';
		$check['month_id'] = $this->get_month_id();
		$check['date_id'] = $this->get_date_id();
		$check['user_id'] = $this->session->userdata('user_id');		
		$time = $this->attendance_model->check_entry_id($check);
		echo $time['id'];
	}

	/*Enter Exit Time for attendance */
	function insert_exit_time()
	{
		$exit['month_id'] = $this->get_month_id();
		$exit['date_id'] = $this->get_date_id();
		$exit['user_id'] = $this->session->userdata('user_id');
		//$exit['exit_time'] = $_REQUEST['time'];
		$exit['exit_time'] = $this->attendance_model->get_server_time();

		echo $this->attendance_model->insert_exit_time($exit);
	}

	function check_exit_time()
	{
		$check['month_id'] = $this->get_month_id();
		$check['date_id'] = $this->get_date_id();
		$check['user_id'] = $this->session->userdata('user_id');
		echo $this->attendance_model->check_exit_time($check);
	}
	
	/*Enter Lunch In Time for attendance */
	function insert_lunch_in_time()
	{
		$insert['month_id'] = $this->get_month_id();
		$insert['date_id'] = $this->get_date_id();
		$insert['user_id'] = $this->session->userdata('user_id');
		//$insert['lunch_in_time'] = $_REQUEST['time'];
$insert['lunch_in_time'] = $this->attendance_model->get_server_time();

		echo $this->attendance_model->insert_lunch_in_time($insert);
	}

	function check_lunch_in_time()
	{
		$check['month_id'] = $this->get_month_id();
		$check['date_id'] = $this->get_date_id();
		$check['user_id'] = $this->session->userdata('user_id');
		echo $this->attendance_model->check_lunch_in_time($check);
	}
	
	/*Enter Lunch Out Time for attendance */
	function insert_lunch_out_time()
	{
		$exit['month_id'] = $this->get_month_id();
		$exit['date_id'] = $this->get_date_id();
		$exit['user_id'] = $this->session->userdata('user_id');
		//$exit['lunch_out_time'] = $_REQUEST['time'];

		$exit['lunch_out_time'] = $this->attendance_model->get_server_time();

		echo $this->attendance_model->insert_lunch_out_time($exit);
	}

	function check_lunch_out_time()
	{
		$check['month_id'] = $this->get_month_id();
		$check['date_id'] = $this->get_date_id();
		$check['user_id'] = $this->session->userdata('user_id');
		echo $this->attendance_model->check_lunch_out_time($check);
	}

	function get_total_working_hours()
	{
		$check = array(
			'month_id'=> $this->get_month_id(),
			'date_id' => $this-> get_date_id(),
			'user_id' => $this->session->userdata('user_id')
		);
		//print_r($data);
		$entry_time = $this->attendance_model->check_entry_time($check);
		$exit_time  = $this->attendance_model->check_exit_time($check);
		$lunch_in_time = $this->attendance_model->check_lunch_in_time($check);
		$lunch_out_time  = $this->attendance_model->check_lunch_out_time($check);
		$entry_time = strtotime($entry_time);
		$exit_time  = strtotime($exit_time);
		$lunch_in_time = strtotime($lunch_in_time);
		$lunch_out_time  = strtotime($lunch_out_time);

		$total_time = ($exit_time - $lunch_out_time) + ($lunch_in_time - $entry_time);
		$hours      = floor($total_time / 3600);
		$minutes    = intval(($total_time / 60) % 60);
		$hours.":".$minutes;
		return $hours.":".$minutes;
		// $this->load->view('user / dashboard',$time,true);
	}

	/**
	*
	*
	*/

	function insert_daily_attendance_sheet()
	{

		$date_column = "day_".$this->get_date_id();
		$total_time  = $this->get_total_working_hours();

		$data      = array(
			'year_id'   	=>	$this->get_year_id(),
			'month_id'  	=>	$this->get_month_id(),
			'user_id'   	=>	$this->session->userdata('user_id'),
			$date_column	=> $total_time
		);

		$sundays = $this->get_sunday();

		//$holidays = ;

		echo $this->attendance_model->insert_daily_attendance_sheet($data,$sundays);
	}

	/*
	function get_attendance_details()
	{
	echo $this->attendance_model->get_attendance_details();
	exit;
	}*/



	function get_sunday()
	{
		$now           = new DateTime();
		$start         = $now->format('Y-m-01');
		$end           = $now->format('Y-m-t');
		$startDate     = strtotime($start);
		$endDate       = strtotime($end);
		$weekdayNumber = 0;
		$dateArr       = array();
		do
		{
			if(date("w", $startDate) != $weekdayNumber){
				$startDate += (24 * 3600); // add 1 day
			}
		} while(date("w", $startDate) != $weekdayNumber);


		while($startDate <= $endDate){
			$dateArr[] = date('d', $startDate);
			$startDate += (7 * 24 * 3600); // add 7 days
		}

		return($dateArr);		
	}
	function get_salary_by_id()
	{
		return $this->attendance_model->get_salary_by_id();
		//print_r($q);

	}
	
	/**
	* @author pratik <pratik@fedobe.org>
	* finding out the 
	* 
*/
	function worksheet_status($user_id,$month_id,$year_id){
		
		//$id = $this->session->userdata('user_id');
		$result = $this->attendance_model->worksheet_status($user_id,$month_id,$year_id);
		return $result;
	}
	
		/**
	* 
	* @param undefined $id
	* @param undefined $month_id
	* @param undefined $year_id
	* @param undefined $type
	* calculate the fine for each month
*/
	
	function fine($id,$month_id,$year_id,$type = NULL){
		$where  = array(
		'user_id' =>$id,
		'month_id' =>$month_id,
		'year_id' =>$year_id,
		);			
	$result = $this->attendance_model->fine($where);	
	
			if($type){
				return $result[$type];
			}else{
				return $result;
			}	
	}
// end of function ...
}

?>